//
//  AppDelegate.m
//  maocuhuiOASystem
//
//  Created by michael on 16/3/20.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "lanViewController.h"

#import "MainTabBarController.h"
#import "ZLCGuidePageView.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "OALoginViewController.h"
#import "AppDelegate+Extendion.h"
#import "MainTabBarController.h"
#import "MenuViewController.h"
#import "LockController.h"
#import "IQKeyboardManager.h"
#import "BaseModel.h"
@interface AppDelegate ()
//<SangforSDKDelegate>


@end
@implementation AppDelegate

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (self.allowRotation) {//如果设置了allowRotation属性，支持全屏
        return UIInterfaceOrientationMaskLandscape;
    }
    //UIInterfaceOrientationMaskLandscape
    //UIInterfaceOrientationMaskAll
    return UIInterfaceOrientationMaskPortrait;//默认全局不支持横屏
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
//第一次启动
}else{
//不是第一次启动了
}
//if(Device_Is_iPhoneX){
//NSLog(@"xxxxxx");
//}
short port = 443;                        //vpn设备端口号，一般为443
NSString *vpnIp =    @"122.224.123.170";  //vpn设备IP地址
 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] ;
  NSArray *images =  @[[UIImage imageNamed:@"1、宣传页面-1（750x1334）"],[UIImage imageNamed:@"1、宣传页面-2（750x1334）"]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"myLanguage"] && ![[[NSUserDefaults standardUserDefaults] objectForKey:@"myLanguage"] isEqualToString:@""]) {
        [NSBundle setLanguage:[[NSUserDefaults standardUserDefaults] objectForKey:@"myLanguage"]];
    }
       NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];

    if([language isEqualToString:@"0"]){
                          [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"website"];


}else{

           [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"website"];

}
//  NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//        NSArray * allLanguages = [defaults objectForKey:@"AppleLanguages"];
//        NSString * preferredLang = [allLanguages objectAtIndex:0];
//        NSLog(@"当前语言:%@", allLanguages);
//
//        if ([preferredLang rangeOfString:@"en"].location != NSNotFound) {
//                             [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"language"];
//                                          [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"website"];
//
//
//        }else if([preferredLang rangeOfString:@"zh-Hans"].location != NSNotFound)
//        {
//             [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"language"];
//                          [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"website"];
//
//
//        }else
//        {
//
//        }


//  if([[self currentLanguage] compare:@"zh-Hans" options:NSCaseInsensitiveSearch]==NSOrderedSame || [[self currentLanguage] compare:@"zh-Hant" options:NSCaseInsensitiveSearch]==NSOrderedSame)
//  {
//     [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"language"];
//
//      lang = @"zh";
//      NSLog(@"current Language == Chinese");
//  }else{
//                 [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"language"];
//
//      lang = @"en";
//      NSLog(@"current Language == English");
//  
//  }
    //创建引导页视图
//    ZLCGuidePageView *pageView = [[ZLCGuidePageView alloc]initWithFrame:self.window.frame WithImages:images];
//
//    if (![[DanLi sharedDanLi].value isEqualToString:@"no"]) {
//            [self.window addSubview:pageView];
//
//}
//     NSArray *images =  @[[UIImage imageNamed:@"1、宣传页面-1（750x1334）"],[UIImage imageNamed:@"1、宣传页面-2（750x1334）"]];
////    //创建引导页视图
//    ZLCGuidePageView *pageView = [[ZLCGuidePageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) WithImages:images];
//        [self.window.rootViewController.view addSubview:pageView];
//[self.window bringSubviewToFront:pageView];

 [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
 [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
 if(![[NSUserDefaults standardUserDefaults]boolForKey:@"firstStart"]){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
//引导页图片数组
  lanViewController *tar = [[lanViewController alloc]init];

                   self.window.rootViewController = tar;

}else{
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"] isEqualToString:@"Y"]) {
MainTabBarController *tar = [[MainTabBarController alloc]init];
// MenuViewController *tar = [[MenuViewController alloc]init];

                   self.window.rootViewController = tar;
                     }else{
                MainTabBarController *tar = [[MainTabBarController alloc]init];

//                      MenuViewController *tar = [[MenuViewController alloc]init];

                   self.window.rootViewController = tar;
//        OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
//        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
//                 self.window.rootViewController = navVC;
//
//        loginVC.navigationController.navigationBar.hidden = YES;
    }
  
}
    [self.window makeKeyAndVisible];
    [self dismissKeyBoard];
    return YES;
}
- (void)changeLanguageTo:(NSString *)language {
    // 设置语言
    [NSBundle setLanguage:language];
    
    // 然后将设置好的语言存储好，下次进来直接加载
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:@"myLanguage"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 我们要把系统windown的rootViewController替换掉
//    ZHTabBarController *tab = [[ZHTabBarController alloc] init];
//    [UIApplication sharedApplication].keyWindow.rootViewController = tab;
//    // 跳转到设置页
//    tab.selectedIndex = 2;
}
-(NSString*)currentLanguage
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLang = [languages objectAtIndex:0];
    return currentLang;
}
//#pragma Mark VPNDelegate
//- (void) onCallBack:(const VPN_RESULT_NO)vpnErrno authType:(const int)authType
//{
//NSString *userName = @"ccpitzjapp";             //用户名认证的用户名
//NSString *password = @"ccpitzjappapp";                //用户名认证的密码
//
//
//    switch (vpnErrno)
//    {
//        case RESULT_VPN_INIT_FAIL:
//            NSLog(@"Vpn Init failed!");
//            break;
//
//        case RESULT_VPN_AUTH_FAIL:
//            [self.helper clearAuthParam:@SET_RND_CODE_STR];
//            NSLog(@"Vpn auth failed!");
//            break;
//
//        case RESULT_VPN_INIT_SUCCESS:
//            NSLog(@"Vpn init success!");
//            //设置认证参数 用户名和密码以数值map的形式传入
//            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_NAME param:userName];
//            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_PASSWORD param:password];
//            //开始用户名密码认证
//            [self.helper loginVpn:SSL_AUTH_TYPE_PASSWORD];
//            break;
//        case RESULT_VPN_AUTH_SUCCESS:
//            [self startOtherAuth:authType];
//            break;
//        case RESULT_VPN_AUTH_LOGOUT:
//            NSLog(@"Vpn logout success!");
//            break;
//        case RESULT_VPN_OTHER:
//            if (VPN_OTHER_RELOGIN_FAIL == (VPN_RESULT_OTHER_NO)authType) {
//                NSLog(@"Vpn relogin failed, maybe network error");
//            }
//            break;
//
//        case RESULT_VPN_NONE:
//            break;
//
//        default:
//            break;
//    }
//}
//
//- (void) startOtherAuth:(const int)authType
//{NSString *userName = @"ccpitzjapp";             //用户名认证的用户名
//NSString *password = @"ccpitzjappapp";                //用户名认证的密码
//
//NSString *certName = @"";     //导入证书名字，如果服务端没有设置证书认证可以不设置
//NSString *certPwd =  @"";          //证书密码，如果服务端没有设置证书
//
//    NSArray *paths = nil;
//    switch (authType)
//    {
//        case SSL_AUTH_TYPE_CERTIFICATE:
//            paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
//                                                        NSUserDomainMask, YES);
//
//            if (nil != paths && [paths count] > 0)
//            {
//                NSString *dirPaths = [paths objectAtIndex:0];
//                NSString *authPaths = [dirPaths stringByAppendingPathComponent:certName];
//                NSLog(@"PATH = %@",authPaths);
//                [self.helper setAuthParam:@CERT_P12_FILE_NAME param:authPaths];
//                [self.helper setAuthParam:@CERT_PASSWORD param:certPwd];
//            }
//            NSLog(@"Start Cert Auth!!!");
//            break;
//
//        case SSL_AUTH_TYPE_PASSWORD:
//            NSLog(@"Start Password Name Auth!!!");
//            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_NAME param:userName];
//            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_PASSWORD param:password];
//
//            break;
//        case SSL_AUTH_TYPE_NONE:
//            NSLog(@"Auth success!!!");
//
//        UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//
//        MainViewController * mainVC = [storyBoard instantiateViewControllerWithIdentifier:@"MainVC"];
//        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
//         self.window.rootViewController = navVC;
//
//            break;
//
////        default:
////            NSLog(@"Other failed!!!");
////            break;
//    }
//    [self.helper loginVpn:authType];
//}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    appDelegate.allowRotation = NO;
//            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//    int ssl_vpn_quit();


//    [_helper logoutVpn];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     
//    appDelegate.allowRotation = NO;
//                [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    appDelegate.allowRotation = NO;
//                [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
//    [_helper logoutVpn];

  


}

@end
